package service

import (
	"comment/api"
	"context"
	"dubbo.apache.org/dubbo-go/v3/config"
	_ "dubbo.apache.org/dubbo-go/v3/imports"
	"encoding/json"
	"github.com/dubbogo/grpc-go/codes"
	"github.com/dubbogo/grpc-go/status"
	"strconv"
	"time"
)

// var db *sqlx.DB
var commonProvider *api.CommonClientImpl

func OK(value []*api.Comment, size int64) (*api.CommentList, error) {
	return &api.CommentList{
		List: value,
		Size: size,
	}, nil
}

func FAIL(err error) (*api.CommentList, error) {
	return nil, err
}

type CommentProvider struct {
	api.UnimplementedCommentProviderServer
}

func (s *CommentProvider) Add(c context.Context, comment *api.Comment) (*api.CommentList, error) {
	// 校验参数
	if comment.GetUserId() == 0 || comment.GetVideoId() == 0 {
		return FAIL(status.Errorf(codes.FailedPrecondition, "parameter error"))
	}

	// 插入数据
	_, err := commonProvider.Update(c, &api.Value{
		Database: "douyin",
		Table:    "comment",
		Key:      []string{"id", "video_id", "user_id", "content", "create_time"},
		Value:    []string{strconv.FormatInt((comment.GetVideoId()<<32)+comment.GetUserId(), 10), strconv.FormatInt(comment.GetVideoId(), 10), strconv.FormatInt(comment.GetUserId(), 10), comment.GetContent(), strconv.FormatInt(time.Now().Unix(), 10)},
	})

	if err != nil {
		return FAIL(err)
	}

	var list []*api.Comment
	list = append(list, &api.Comment{
		Id:         0,
		UserId:     0,
		VideoId:    0,
		Content:    "",
		CreateTime: 0,
	})
	return OK(list, 0)
}
func (s *CommentProvider) Del(c context.Context, comment *api.Comment) (*api.CommentList, error) {
	// 参数校验
	if comment.GetId() == 0 {
		return FAIL(status.Errorf(codes.FailedPrecondition, "parameter error"))
	}

	// 删除
	_, err := commonProvider.Del(c, &api.Query{
		Database: "douyin",
		Table:    "comment",
		Where: []*api.Query_Condition{
			{
				Key:   []string{"id"},
				Value: []string{strconv.FormatInt(comment.GetId(), 10)},
				Type:  []api.Query_Condition_ConditionType{api.Query_Condition_NE},
			},
		},
	})

	if err != nil {
		return FAIL(err)
	}

	return OK(nil, 0)
}
func (s *CommentProvider) Find(c context.Context, comment *api.Comment) (*api.CommentList, error) {
	// 参数校验
	if comment.GetVideoId() == 0 {
		return FAIL(status.Errorf(codes.FailedPrecondition, "parameter error"))
	}

	// 查询
	get, err := commonProvider.Get(c, &api.Query{
		Database: "douyin",
		Table:    "comment",
		Where: []*api.Query_Condition{
			{
				Key:   []string{"video_id"},
				Value: []string{strconv.FormatInt(comment.GetVideoId(), 10)},
				Type:  []api.Query_Condition_ConditionType{api.Query_Condition_NE},
			},
		},
		OrderBy: []*api.Query_Order{
			{
				Key:  "create_time",
				Type: api.Query_Order_DESC,
			},
		},
		Result: api.Query_COMMENT,
	})

	if err != nil {
		return FAIL(err)
	}

	var result []*api.Comment
	err = json.Unmarshal(get.Value, &result)
	if err != nil {
		return FAIL(err)
	}
	return OK(result, 0)
}
func (s *CommentProvider) Count(c context.Context, comment *api.Comment) (*api.CommentList, error) {
	// 参数校验
	if comment.GetVideoId() == 0 {
		return FAIL(status.Errorf(codes.FailedPrecondition, "parameter error"))
	}

	// 删除
	count, err := commonProvider.Count(c, &api.Query{
		Database: "douyin",
		Table:    "comment",
		Where: []*api.Query_Condition{
			{
				Key:   []string{"video_id"},
				Value: []string{strconv.FormatInt(comment.GetVideoId(), 10)},
				Type:  []api.Query_Condition_ConditionType{api.Query_Condition_NE},
			},
		},
	})

	if err != nil {
		return FAIL(err)
	}

	return OK(nil, count.Size)
}

func init() {
	// 公共服务
	commonProvider = new(api.CommonClientImpl)

	// 注册服务
	config.SetProviderService(&CommentProvider{})
	config.SetConsumerService(commonProvider)
}
